package com.qing.provider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qing.api.pojo.User;
import com.qing.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

// 提供RestFul 服务
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod ="hystrixGetUser")
    public User findById(@PathVariable("id") Long id){
        User user=userService.queryById(id);
        if(user==null){
            throw  new RuntimeException("这个用户不存在！");
        }
        return  user;
    }


    //备选方案
    public User hystrixGetUser(@PathVariable("id") Long id){
        return new User().setId(id).setName("hystrixGetUser").setDbSource("not db");
    }
}
